﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.MountAndBlade.View.Screens.Scripts.MultiThreadedStressTestsScreen
// Assembly: TaleWorlds.MountAndBlade.View, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 61152EB8-513B-4D4A-8AC0-0D1465C6CF6C
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\Modules\Native\bin\Win64_Shipping_Client\TaleWorlds.MountAndBlade.View.dll

using System;
using System.Collections.Generic;
using System.Threading;
using TaleWorlds.Engine;
using TaleWorlds.Library;
using TaleWorlds.ScreenSystem;

#nullable disable
namespace TaleWorlds.MountAndBlade.View.Screens.Scripts
{
  public class MultiThreadedStressTestsScreen : ScreenBase
  {
    private List<Thread> _workerThreads;
    private Scene _scene;
    private SceneView _sceneView;

    protected override void OnActivate()
    {
      base.OnActivate();
      this._scene = Scene.CreateNewScene();
      this._scene.Read("mp_ruins_2");
      this._sceneView = SceneView.CreateSceneView();
      this._sceneView.SetScene(this._scene);
      this._sceneView.SetSceneUsesShadows(true);
      Camera camera = Camera.CreateCamera();
      camera.Frame = this._scene.ReadAndCalculateInitialCamera();
      this._sceneView.SetCamera(camera);
      this._workerThreads = new List<Thread>();
      this._workerThreads.Add(new Thread((ThreadStart) (() => MultiThreadedStressTestsScreen.MultiThreadedTestFunctions.MeshMerger(InputLayout.Input_layout_regular)))
      {
        Name = "StressTester|Mesh Merger Thread"
      });
      this._workerThreads.Add(new Thread((ThreadStart) (() => MultiThreadedStressTestsScreen.MultiThreadedTestFunctions.MeshMerger(InputLayout.Input_layout_normal_map)))
      {
        Name = "StressTester|Mesh Merger Thread"
      });
      this._workerThreads.Add(new Thread((ThreadStart) (() => MultiThreadedStressTestsScreen.MultiThreadedTestFunctions.MeshMerger(InputLayout.Input_layout_skinning)))
      {
        Name = "StressTester|Mesh Merger Thread"
      });
      for (int index = 0; index < this._workerThreads.Count; ++index)
        this._workerThreads[index].Start();
    }

    protected override void OnDeactivate()
    {
      base.OnDeactivate();
      this._sceneView = (SceneView) null;
      this._scene = (Scene) null;
    }

    protected override void OnFrameTick(float dt)
    {
      base.OnFrameTick(dt);
      bool flag = true;
      for (int index = 0; index < this._workerThreads.Count; ++index)
      {
        if (this._workerThreads[index].IsAlive)
          flag = false;
      }
      if (!flag)
        return;
      ScreenManager.PopScreen();
    }

    public static class MultiThreadedTestFunctions
    {
      public static void MeshMerger(InputLayout layout)
      {
        int inputLayout = (int) layout;
        Mesh copy1 = Mesh.GetRandomMeshWithVdecl(inputLayout).CreateCopy();
        UIntPtr num = copy1.LockEditDataWrite();
        Mesh copy2 = Mesh.GetRandomMeshWithVdecl(inputLayout).CreateCopy();
        Mesh randomMeshWithVdecl1 = Mesh.GetRandomMeshWithVdecl(inputLayout);
        Mesh randomMeshWithVdecl2 = Mesh.GetRandomMeshWithVdecl(inputLayout);
        copy1.AddMesh(randomMeshWithVdecl1, MatrixFrame.Identity);
        copy2.AddMesh(randomMeshWithVdecl2, MatrixFrame.Identity);
        copy1.AddMesh(copy2, MatrixFrame.Identity);
        int patchNode0 = copy1.AddFaceCorner(new Vec3(z: 1f), new Vec3(z: 1f), new Vec2(0.0f, 1f), 268435455U, num);
        int patchNode1 = copy1.AddFaceCorner(new Vec3(y: 1f), new Vec3(z: 1f), new Vec2(1f, 0.0f), 268435455U, num);
        int patchNode2 = copy1.AddFaceCorner(new Vec3(y: 1f, z: 1f), new Vec3(z: 1f), new Vec2(1f, 1f), 268435455U, num);
        copy1.AddFace(patchNode0, patchNode1, patchNode2, num);
        copy1.UnlockEditDataWrite(num);
      }

      public static void SceneHandler(SceneView view)
      {
        int num = 0;
        while (num < 500)
        {
          view.SetSceneUsesShadows(false);
          view.SetRenderWithPostfx(false);
          Thread.Sleep(5000);
          view.SetSceneUsesShadows(true);
          view.SetRenderWithPostfx(true);
          Thread.Sleep(5000);
          view.SetSceneUsesContour(true);
          Thread.Sleep(5000);
        }
      }
    }
  }
}
